package com.p2p.backend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.backend.service.BackBankService;
import com.p2p.backend.service.OplogService;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@RequestMapping("/bank")
@Controller
public class BankController extends BaseController {

	@Resource
	private BackBankService bankService;

	// 银行不会太多，不是先分页，“伪分页”，手动设置“total”字段
	@PermissionNeed("bank_list")
	@RequestMapping("list")
	public void list(Model model) {
		List<Map<String, Object>> list = bankService.listAll();
		model.addAttribute("res", 1);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("rows", list);
		data.put("total", list.size());
		model.addAttribute("data", data);

	}

	@PermissionNeed("bank_management")
	@RequestMapping("doadd")
	public void doadd(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		bankService.add(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK);
		oplog.setAction(Action.ADD);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("bank_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer id, Model model) {
		Map<String, Object> bank = bankService.get(id);
		model.addAttribute("bank", bank);
		return "bank/edit";
	}

	@PermissionNeed("bank_management")
	@RequestMapping("doupdate")
	public void doupdate(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		bankService.update(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK);
		oplog.setAction(Action.UPDATE);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("bank_management")
	@RequestMapping("unpass")
	public void unpass(HttpServletRequest request, @RequestParam Integer id,
			ModelMap model) {
		bankService.unpass(id);
		model.addAttribute("res", 1);
		unpassOplog(request, id);
	}

	@PermissionNeed("bank_management")
	@RequestMapping("pass")
	public void pass(HttpServletRequest request, @RequestParam Integer id,
			Model model) {
		bankService.pass(id);
		model.addAttribute("res", 1);
		passOplog(request,id);
	}
	
	private void unpassOplog(HttpServletRequest request, Integer id) {
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK);
		oplog.setAction(Action.AUDIT_REJECT);
		String params = "id=" + id;
		oplog.setParams(params);
		OplogService.log(oplog);
	}
	
	private void passOplog(HttpServletRequest request, Integer id) {
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.BANK);
		oplog.setAction(Action.AUDIT_PASS);
		String params = "id=" + id;
		oplog.setParams(params);
		OplogService.log(oplog);
	}
}
